<?php
/**
 * 装修公司
 */
namespace App\Http\Controllers;

use App\Service\ScrapyEffService;
use App\Validation\ScrapyEffValidation;

class ScrapyEffController extends BaseController
{
	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser(0);
		$raw = $this->validation(new ScrapyEffValidation, $this->request->getJsonRawBody());

		$data = ScrapyEffService::getList($raw);
		$d = [];
		$type_arr = [
			'ershoufang' => '二手房',
			'zufang'	 => '租房',
			'xinfang'	 => '新房',
		];
		foreach ($data as $k => $v) {
			$group = $v->ip . '-' . $v->add_time . '-' . $v->sources . '-' . $v->type;
			$md5 = md5($group);
			$d[$md5] = isset($d[$md5]) ? $d[$md5] : [];
			// $d[$md5]['md5'] = $md5;
			// $d[$md5]['id'] = $v->id;
			$d[$md5]['ip'] = $v->ip;
			$d[$md5]['add_time'] = $v->add_time;
			// $d[$md5]['is_eff'] = $v->is_eff ? '有效' : '无效';
			$d[$md5]['sources'] = $v->sources;
			$d[$md5]['type'] = in_array($v->type, array_keys($type_arr)) ? $type_arr[$v->type] : "";
			$d[$md5]['total']  = isset($d[$md5]['total']) ? $d[$md5]['total'] + $v->num : $v->num;
			if ($v->is_eff == 1)
				$d[$md5]['num']  = isset($d[$md5]['num']) ? $d[$md5]['num'] + $v->num : $v->num;
		}
		$d = array_values($d);
		$count = ScrapyEffService::count();

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}
}